package com.jstyle.beatecg.activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.jstyle.beatecg.NetWork.NetWorkUtil;
import com.jstyle.beatecg.R;
import com.jstyle.beatecg.base.BaseActivity;
import com.jstyle.beatecg.model.NetResultData;
import com.jstyle.beatecg.util.NetWorkConast;
import com.jstyle.beatecg.util.ScreenUtils;
import java.util.HashMap;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/10.
 */

public class FeedBackActivity extends BaseActivity {
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.bt_feed_back)
    ImageView btFeedBack;
    @BindView(R.id.et_advice_text)
    EditText etAdviceText;
    @BindView(R.id.et_advice_email)
    EditText etAdviceEmail;
    @BindView(R.id.bt_advice)
    Button btAdvice;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advice);
        ButterKnife.bind(this);
        ScreenUtils.setStatusBarColor(llTitle, Color.TRANSPARENT);
    }

    @OnClick({R.id.bt_feed_back, R.id.bt_advice})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_feed_back:
                finish();
                break;
            case R.id.bt_advice:
                submitAdvice();
                break;
        }
    }
    private void submitAdvice() {
        if(!NetWorkUtil.isNetworkConnected(this)){
            showToast(getString(R.string.Network_not_availa));
            return;
        }
        // TODO Auto-generated method stub
        final String adviceText = etAdviceText.getText().toString();
        final String email = etAdviceEmail.getText().toString();
        if (TextUtils.isEmpty(email) || TextUtils.isEmpty(adviceText)) {
            Toast.makeText(this,
                    getResources().getString(R.string.fill_all_info),
                    Toast.LENGTH_SHORT).show();
            return;
        }
        etAdviceText.setText("");
        Map<String,String> feedBackMap=new HashMap<>();
        feedBackMap.put(NetWorkConast.KEY_token,NetWorkUtil.getToken());
        feedBackMap.put(NetWorkConast.KEY_email,email);
        feedBackMap.put(NetWorkConast.KEY_Content,adviceText);
        feedBackMap.put(NetWorkConast.KEY_Content,adviceText);
        feedBackMap.put(NetWorkConast.KEY_devicetype,"1637");
        feedBackMap.put(NetWorkConast.KEY_AppName,"JstyleSleep");
        feedBackMap.put(NetWorkConast.KEY_osType,"Android");
        feedBackMap.put(NetWorkConast.KEY_AppVersion,getAppVersion());
        NetWorkUtil.getInstance().getJstyleApi().feedBack(feedBackMap).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
                                                                                                                       @Override
                                                                                                                       public void accept(Disposable disposable) throws Exception {
                                                                                                                           showProgressDialog(getString(R.string.Submitting));
                                                                                                                       }
                                                                                                                   }

        ).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<NetResultData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(NetResultData value) {
                disMissProgressDialog();
                int  resultCode=value.getMsgCode();
                String message="";
                if (resultCode==1) {
                    message = getString(R.string.feedback_successful);
                } else if (resultCode==1221) {
                    message = getString(R.string.email_format_wrong);
                }
                showToast(message);
            }

            @Override
            public void onError(Throwable e) {
                disMissProgressDialog();
                showToast(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });

    }


    public String getAppVersion() {
        PackageInfo packageInfo;
        String versionName="";
        try {
            packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            versionName = packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return  versionName;

    }
}
